﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Linq
{
    /// <summary>
    /// Linq相关的扩展方法
    /// </summary>
    public static class LinqExtensions
    {
        /// <summary>
        /// 从集合中返回第一条，没有集合为空返回新的实体
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns> 
        public static TSource FirstOrNewEntity<TSource>(this IEnumerable<TSource> source) where TSource : new()
        {
            if (source == null || !source.Any())
                return new TSource();
            return source.FirstOrDefault();
        }
    }
}
